<!DOCTYPE html>
<html lang="zh-cn" class="no-js">
	<head>
		<meta http-equiv="Content-Type">
		<meta content="text/html; charset=utf-8">
		<meta charset="utf-8">
		<title>个人中心</title>		
		<meta name="viewport" content="target-densitydpi=device-dpi, width=750px, user-scalable=no">
		<meta name="format-detection" content="telephone=no">
		<meta name="format-detection" content="email=no">
		<script src="/zghpt/js/jquery-3.1.0.min.js"></script>
		<script type="text/javascript">
		function adaptscreen(widthnum){
			try{
			 var DEFAULT_WIDTH = widthnum, // 页面的默认宽度
		        ua = navigator.userAgent.toLowerCase(), // 根据 user agent 的信息获取浏览器信息
		        deviceWidth = window.screen.width, // 设备的宽度
		        devicePixelRatio = window.devicePixelRatio || 1, // 物理像素和设备独立像素的比例，默认为1
		        targetDensitydpi;    // Android4.0以下手机不支持viewport的width，需要设置target-densitydpi
		        if (ua.indexOf("android") !== -1 && parseFloat(ua.slice(ua.indexOf("android")+8)) < 4) {
		        	targetDensitydpi = DEFAULT_WIDTH / deviceWidth * devicePixelRatio * 160;
		        	$('meta[name="viewport"]').attr('content', 'target-densitydpi=' + targetDensitydpi + ', width=device-width, user-scalable=no');
				}else{
					$('meta[name="viewport"]').attr('content', 'target-densitydpi=device-dpi, width='+DEFAULT_WIDTH+'px, user-scalable=no');
				}
			}catch(e){
				console.log('dpi error',e);
			}
	    }
	    adaptscreen(750);
		</script>
		<link rel="stylesheet" type="text/css" href="/zghpt/css/reset.css?v=6" />
		<link rel="stylesheet" type="text/css" href="/zghpt/css/usercenter.css?v=6" />
		<link rel="stylesheet" type="text/css" href="/zghpt/css/animations.css?v=6" />
        <link rel="stylesheet" type="text/css" href="/zghpt/css/load.css?v=6" /> 
	</head>
	<body>

    <div id="loading">
     	<div class="spinner">
  			<div class="double-bounce1"></div>
  			<div class="double-bounce2"></div>
		</div>
	</div>

    <div id="mask"></div>

	<div id="content" style="display:none">
    </div>

    <!-- dialog-userInfoLayer start -->
    <div class="dialog dialog-userInfoLayer">
		<div class="dialog-mask no-event"></div>
		<div class="dialog-content">
		
			<div class="userInfo">
				<div class="userInfo-bg-top"></div>
				<div class="userInfo-content">
					<div class="userInfo-content-inn">

						<div class="cnt-info">
							<div class="user-head">
								<img src="" id="userHead" class="head" />
								<div class="frame"></div>
							</div>
							<div class="user-info">
								<div class="name" id="userName"></div>
								<span>小妖币：<b id="xyScore"></b></span>
								<span>金币：<b id="goldCoin"></b></span>
							</div>
						</div>

						<div class="cnt-operator">
							<ul>
								<li class="js-btn-exchange">
									<a href="javascript:;">
										<span class="title text-shadow-1 bold">兑换</span>
										<span class="text text-shadow-1"><i id="canExchange"></i>元</span>
										<span class="arrow"></span>
									</a>
								</li>
								<li class="js-btn-showRechargeLayer">
									<a href="javascript:;" >
										<span class="title text-shadow-1 bold">充值</span>
										<span class="arrow"></span>
									</a>
								</li>
								<!--<li class="js-btn-friendsList">
									<a href="javascript:;">
										<span class="title text-shadow-1 bold">好友</span>
										<span class="text text-shadow-1" id="fansCount"></span>
										<span class="arrow"></span>
									</a>
								</li>-->
								<li class="js-create-qrcode">
									<a href="javascript:;">
										<span class="title text-shadow-1 bold">二维码</span>
										<span class="arrow"></span>
									</a>
								</li>
							</ul>
						</div>

					</div>
				</div>
			</div>

		</div>
    </div>
    <!-- dialog-userInfoLayer end -->

    <!-- dialog-recharge start -->
    <div class="dialog dialog-recharge hide">
		<div class="dialog-mask"></div>
		<div class="dialog-content">
			<input type="number" class="input-area" id="rechargeMoney" placeholder="请输入金额（元）" />
			<a href="javascript:;" class="submit text-shadow-1 js-btn-recharge">确定</a>
		</div>
    </div>
    <!-- dialog-recharge end -->

    <!-- form start -->
    <form id="myForm" class="hide" action="" method="post">
        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
        <input type="hidden" name="user" id="hiddenUser" />
        <input type="hidden" name="url" id="hiddenURL" />
        <input type="submit" value="Submit" />
    </form>
    <!-- form end -->

    <script type="text/javascript" src="/zghpt/js/fastclick.js"></script>
    <!--<script type="text/javascript" src="/zghpt/js/jquery.cookie.js"></script>-->
    <script type="text/javascript">
	document.onreadystatechange = loading;
	var loading_pc_num=0;
	var loading_pc=setInterval(function(){
		loading_pc_num+=parseInt(Math.random()*6);
		$('.loading_main').html(loading_pc_num+'%');
		if(loading_pc_num>90){
			clearInterval(loading_pc);
		}
	},300);

	function loading(){
		if(document.readyState == "complete"){
			loading_pc_num=94;
			setTimeout(function(){
				startpage();
			},100);
		}
	}

	function startpage(){
		$("#loading").hide();
		$("#content").show();
		//goNext();
	}

	$(function(){
        FastClick.attach(document.body);

        getUserInfo();

		// 公用弹层事件
		(function() {
			var $dialogMask = $('.dialog-mask').not(".no-event");
			var $close = $('.dialog-close');

			// mask
			$dialogMask.on('click', function() {
				$(this).parents('.dialog').addClass('hide');
			});

			// close
			$close.on('click', function() {
				$(this).parents('.dialog').addClass('hide');
			});
		}());

        // 兑换
        $('.js-btn-exchange').on('click', function() {
            exchange();
        });

		// 弹出充值弹层
		$('.js-btn-showRechargeLayer').on('click', function() {
			$('.dialog-recharge').removeClass('hide');
		});

		// 充值确定按钮
		$('.js-btn-recharge').on('click', function() {
			var money = $('#rechargeMoney').val();
            var r = /^[0-9]*[1-9][0-9]*$/; //正整数 

			if ( !r.test(money) ) {
				alert('请输入正确的金额');
			} else {
				pay(money);
			}
		});

        // 生成二维码
        $('.js-create-qrcode').on('click', function() {
            alert('二维码正在生成中，稍后发到您手机');
			$.ajax({
				type: "get",
				url: "/api/user/qrcode",
				data: {
				},
				success: function(jdata){
					// if (jdata.code == 1000) {
						// alert('二维码正在生成中，稍微发到您手机');
					// } else {
					// 	alert(jdata.msg);
					// }
				},
				error: function(){
				}
			});
        });

        // 兑换
        function exchange() {
            $("#loading").show();
            $("#mask").show();
            $.ajax({
                type: "post",
                url: "/api/third/wechat/lucky-money",
                data: {
                },
                success: function(jdata){
                    $("#loading").hide();
                    $("#mask").hide();
                    if (jdata.code == 1000) {

                        // 发起微信支付
                        $("#loading").hide();
                        $("#mask").hide();

                        $("#xyScore").html(jdata.results.XYB);
                        $("#canExchange").html(jdata.results.XYB);
                        
                        alert('提现成功');

                    } else if (jdata.code == 9000) {
                        alert('系统繁忙，请稍后再试');
                    } else if (jdata.code == 9001) {
                        rulesBind(location.href, jdata.results.user, '/third/wechat/auth-payment');
                    } else if (jdata.code == 9002 || jdata.code == 9002) {
                        alert(jdata.msg);
                    }
                },
                error: function(){
                }
            });
        }

        // 发起支付
        function pay(money) {
            $("#loading").show();
            $("#mask").show();
            $.ajax({
                type: "get",
                url: "/api/third/wechat/prepare",
                data: {
                    charge: money
                },
                success: function(jdata){
                    $("#loading").hide();
                    $("#mask").hide();
                    if (jdata.code == 1000) {
                        // 发起微信支付
                        $('.dialog-recharge').addClass('hide');
                        $('#rechargeMoney').val('');
                        wxPay( $.parseJSON(jdata.results), money );
                    } else if (jdata.code == 9000) {
                        alert('系统繁忙，请稍后再试');
                    } else if (jdata.code == 9001) {
                        $('.dialog-recharge').addClass('hide');
                        $('#rechargeMoney').val('');
                        rulesBind(location.href, jdata.results.user, '/third/wechat/auth-receipt');
                    }
                },
                error: function(){
                }
            });
        }

        // 发起权限绑定
        function rulesBind(callbackURL, user, actionURL) {
            $('#myForm').attr('action', actionURL);
            $('#hiddenUser').val(user);
            $('#hiddenURL').val(callbackURL);
            $('#myForm').submit();
        }

        // 发起微信支付
        function wxPay(jsStr, money) {
            $("#loading").show();
            $("#mask").show();
            money = Number(money);
            console.log(jsStr);
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', jsStr,
                function(res){
                    $("#loading").hide();
                    $("#mask").hide();
        //            alert(res.err_msg);
                    if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                        //成功
                        // console.log('成功');
                        var canExchangeTotal = money + Number( $("#canExchange").html() );
                        var xyScoreTotal = money + Number( $("#xyScore").html() );
                        
                        $("#xyScore").html(xyScoreTotal);
                        $("#canExchange").html(canExchangeTotal);
                    } else {
                        alert('充值失败，请重新发起！');
                    }
                }
            );
        }

		// 获取用户信息
		function getUserInfo() {
            $.ajax({
                type: "get",
                url: "/api/user/center",
                data: {
                },
                success: function(jdata){
					if (jdata.code == 1000) {

                        var data = jdata.data;
                        $('#userHead').attr('src', data.avatar);
                        $('#userName').html(data.nickname);
                        $('#goldCoin').html(data.JB);
                        $('#xyScore').html(data.XYB);
                        $('#canExchange').html(data.XYB);
                        $('#fansCount').html(data.fan_count);
                        
					} else {
						alert(jdata.msg);
					}
                },
                error: function(){
                }
            });
		}

	});
	</script>
	</body>
</html>
